package gui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import robot.DBComm;

public class Main extends JPanel {

    public Color orange = new Color(238, 182, 35);
    public Color maroon = new Color(136, 58, 58);
    Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    private int delay = 40;
    private BufferedImage image;
    private boolean working = true;
    public static Client client = new Client();
    int min;
    float alpha = 1f;
    int width;
    int height;
    private boolean cancel = false;
    public JPanel rightSide = new JPanel(new GridLayout(2, 0));
    public static VideoPane videoPane;

    public Main() {

        this.setFocusable(true);
        addKeyListener(new java.awt.event.KeyAdapter() {

            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });
        this.setLayout(new GridLayout(0, 1));
        this.setDoubleBuffered(true);
        WaitingThread w = new WaitingThread();
        new Thread(w).start();
    }

    public void initComponents() {
        videoPane = new VideoPane();
        rightSide.add(criep.CRIEP.PhysicalArena.getGrid());
        rightSide.add(videoPane);
        client.jTabPane.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent changeEvent) {
                JTabbedPane sourceTabbedPane = (JTabbedPane) changeEvent.getSource();
                int index = sourceTabbedPane.getSelectedIndex();
                if (index == 0) {
                    client.jspSplitPane.setRightComponent(rightSide);
                    Client.btnCommandSubmit.setEnabled(true);
                } else if (index == 1) {
                    client.jspSplitPane.setRightComponent(criep.CRIEP.VirtualArena.getGrid());
                    Client.btnCommandSubmit.setEnabled(true);
                } else {
                    client.jspSplitPane.setRightComponent(new Credits());
                    client.repaint();
                    Client.btnCommandSubmit.setEnabled(false);
                }
            }
        });

        client.jTabPane.setSelectedIndex(0);
        client.jspSplitPane.setRightComponent(rightSide);
        criep.CRIEP.frame.setResizable(true);
        screenDim.setSize(screenDim.getWidth() - 100, screenDim.getHeight() - 100);
        criep.CRIEP.frame.setSize(screenDim);
        criep.CRIEP.frame.setLocation(50, 60);
        add(client);
    }

    private void formKeyPressed(java.awt.event.KeyEvent evt) {
        cancel = true;
    }

    class WaitingThread implements Runnable {

        public void run() {
            while (getWidth() == 0 || getHeight() == 0) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException ex) {
                }
            }
            width = getWidth();
            height = getHeight();
            LoadingThread l = new LoadingThread();
            new Thread(l).start();
            while (working) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException ex) {
                }
            }
            if (!cancel) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException ex) {
                }
            }
            repaint();
            initComponents();
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        if (getWidth() != 0 && getHeight() != 0) {
            width = getWidth();
            height = getHeight();
        }

        if (!working && !cancel) {
            if (alpha < 0f) {
                alpha = 0f;
            }
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
            g2d.drawImage(image, width / 2 - min / 2, 0, null);
        }
    }

    class LoadingThread implements Runnable {

        public void run() {
            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = (Graphics2D) getGraphics();

            try {
                Graphics2D g2d = image.createGraphics();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                        RenderingHints.VALUE_INTERPOLATION_BICUBIC);
                g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                        RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                        RenderingHints.VALUE_RENDER_QUALITY);
                min = Math.min(width, height);
                int scalar = min / 7;

                g2.clearRect(0, 0, width, height);

                g2.drawImage(image, width / 2 - min / 2, 0, null);

                for (int i = 0; i < 360; i += 9) {
                    if (cancel) {
                        break;
                    }
                    if (i % 2 == 0) {
                        g2d.setColor(maroon);
                    } else {
                        g2d.setColor(orange);
                    }
                    g2d.fillArc((int) (.6 * scalar), (int) (.6 * scalar), min - (int) (.6 * scalar) * 2, min - (int) (.6 * scalar) * 2, i, 9);
                    g2d.setColor(maroon);
                    g2d.fillOval((int) (.9 * scalar), (int) (.9 * scalar), min - (int) (.9 * scalar) * 2, min - (int) (.9 * scalar) * 2);
                    g2d.setColor(orange);
                    g2d.drawOval((int) (.9 * scalar), (int) (.9 * scalar), min - (int) (.9 * scalar) * 2, min - (int) (.9 * scalar) * 2);
                    g2.drawImage(image, width / 2 - min / 2, 0, null);
                    Thread.sleep(delay);
                }
                g2d.setColor(orange);
                g2d.drawOval((int) (.6 * scalar), (int) (.6 * scalar), min - (int) (.6 * scalar) * 2, min - (int) (.6 * scalar) * 2);
                g2d.setColor(maroon);
                g2d.fillOval((int) (.9 * scalar), (int) (.9 * scalar), min - (int) (.9 * scalar) * 2, min - (int) (.9 * scalar) * 2);
                g2d.setColor(orange);
                g2d.drawOval((int) (.9 * scalar), (int) (.9 * scalar), min - (int) (.9 * scalar) * 2, min - (int) (.9 * scalar) * 2);

                for (double i = 22.5; i < 360; i += 45) {
                    if (cancel) {
                        break;
                    }
                    drawPolygon(g2d, i, .5, width, height);
                    g2.drawImage(image, width / 2 - min / 2, 0, null);
                    Thread.sleep(delay);
                }
                for (int i = 45; i < 360; i += 90) {
                    if (cancel) {
                        break;
                    }
                    drawPolygon(g2d, i, .75, width, height);
                    g2.drawImage(image, width / 2 - min / 2, 0, null);
                    Thread.sleep(delay);
                }
                for (int i = 0; i < 360; i += 90) {
                    if (cancel) {
                        break;
                    }
                    drawPolygon(g2d, i, 1, width, height);
                    g2.drawImage(image, width / 2 - min / 2, 0, null);
                    Thread.sleep(delay);
                }
                g2.drawImage(image, width / 2 - min / 2, 0, null);
                //g2.setFont(new Font("Arial",Font.BOLD,50));
                //g2.setColor(Color.BLACK);
                //g2.drawString("Armstrong Atlantic State University", getWidth()/2 - g2.getFontMetrics().stringWidth("Armstrong Atlantic State University")/2, getHeight()-100);

                Thread.sleep(200);
                working = false;
                alpha = 1.0f;
                while (alpha > 0) {
                    repaint();
                    alpha -= 0.025f;
                    Thread.sleep(delay);
                }
                alpha = 0.0f;
                updateUI();
            } catch (java.lang.InterruptedException ex) {
            }
        }
    }

    public void drawPolygon(Graphics2D g, double angle, double size, int w, int h) {
        double radian = angle * (Math.PI / 180);
        int min = Math.min(w, h);
        int center = min / 2;
        int scalar = min / 7;
        Polygon p1 = new Polygon();
        Polygon p2 = new Polygon();

        // Point at the center
        p1.addPoint(center, center);
        p2.addPoint(center, center);

        // The longest point
        int hyp = (int) (3.5 * scalar);
        int longX = (int) (center + Math.cos(radian) * hyp * size);
        int longY = (int) (center + Math.sin(radian) * hyp * size);
        p1.addPoint(longX, longY);
        p2.addPoint(longX, longY);

        // The shortest point
        double adjustedRad = 45 * (Math.PI / 180);
        hyp = (int) (.67 * scalar);
        int maroonX = center + (int) ((Math.cos(radian + adjustedRad)) * hyp * size);
        int maroonY = center + (int) ((Math.sin(radian + adjustedRad)) * hyp * size);
        p1.addPoint(maroonX, maroonY);

        int orangeX = center + (int) ((Math.cos(radian - adjustedRad)) * hyp * size);
        int orangeY = center + (int) ((Math.sin(radian - adjustedRad)) * hyp * size);
        p2.addPoint(orangeX, orangeY);

        g.setColor(maroon);
        g.fillPolygon(p2);
        g.drawPolygon(p2);
        g.setColor(orange);
        g.fillPolygon(p1);
        g.drawPolygon(p1);
        g.drawPolygon(p2);

    }
}
